<?php
namespace app\daohang\loglic;

use app\daohang\loglic\Form;

class Category extends Form
{
    /**
    * 定义栏目模型的字段
    * @version 1.0.0 首次引入
    * @param array $data 可选;初始数据;默认：空
    * @return array 表格列字段属性（DcBuildTable）
    */
    public function fields($data)
    {
        $fields = [
            'html_1' => [
                'order'           => 0,
                'type'            => 'html',
                'value'           => '<div class="row"><div class="col-12 col-md-8">',
            ],
            'term_type' => [
                'order'           => 0,
                'type'            => 'hidden',
                'value'           => 'category',
            ],
            /*'term_action' => [
                'type'            => 'hidden',
                'value'           => 'index',
                'order'           => 0,
            ],
            'term_controll' => [
                'type'            => 'hidden',
                'value'           => 'category',
                'order'           => 0,
            ],*/
            'term_module' => [
                'order'           => 0,
                'type'            => 'hidden',
                'value'           => 'daohang',
            ],
            'term_id' => [
                'order'           => 1,
                'type'            => 'hidden',
                'value'           => $data['term_id'],
                'data-filter'     => false,
                'data-visible'    => true,
                'data-width'      => 80,
                'data-width-unit' => 'px',
                'data-sortable'   => true,
            ],
            'term_name' => [
                'order'           => 2,
                'type'            => 'text',
                'value'           => $data['term_name'],
                'required'        => true,
                'data-filter'     => false,
                'data-visible'    => true,
                'data-align'      => 'left',
            ],
            'term_slug' => [
                'order'           => 3,
                'type'            => 'text',
                'value'           => $data['term_slug'],
                'data-width'      => 200,
                'data-width-unit' => 'px',
                'data-filter'     => false,
                'data-visible'    => true,
                'data-align'      => 'left',
            ],
            'term_image' => [
                'order'           => 4,
                'type'            => 'image',
                'value'           => $data['term_image'],
                'title'           => lang('dh_term_image'),
            ],
            'term_info' => [
                'order'           => 8,
                'type'            => 'text',
                'value'           => $data['term_info'],
                'data-filter'     => false,
                'data-visible'    => false,
            ],
            'term_title' => [
                'order'           => 5,
                'type'            => 'text',
                'value'           => $data['term_title'],
                'title'           => lang('dh_term_title'),
            ],
            'term_keywords' => [
                'order'           => 6,
                'type'            => 'text',
                'value'           => $data['term_keywords'],
                'title'           => lang('dh_term_keywords'),
            ],
            'term_description' => [
                'order'           => 7,
                'type'            => 'textarea',
                'value'           => $data['term_description'],
                'title'           => lang('dh_term_description'),
                'rows'            => 3,
            ],
            'html_2'      => [
                'order'           => 9,
                'type'            => 'html',
                'value'           => '</div><div class="col-12 col-md-4">',
            ],
            'term_parent' => [
                'order'           => 10,
                'type'            => 'select',
                'value'           => $data['term_parent'],
                'option'          => DcTermOption(['module'=>['eq','daohang']]),
                'data-width'      => 100,
                'data-filter'     => false,
                'data-visible'    => true,
            ],
            'term_status' => [
                'order'           => 11,
                'type'            => 'select',
                'value'           => DcEmpty($data['term_status'],'normal'),
                'option'          => ['normal'=>lang('normal'),'hidden'=>lang('hidden'),'private'=>lang('private')],
                'data-filter'     => true,
                'data-visible'    => false,
            ],
            'term_status_text' => [
                'order'           => 11,
                'data-title'      => lang('term_status'),
                'data-visible'    => true,
                'data-width'      => 100,
            ],
            'term_navbar'=> [
                'order'            => 12,
                'type'             => 'custom',
                'value'            => $data['term_navbar'],
                'option'           => ['no'=>'no','yes'=>'yes'],
                'title'            => lang('dh_term_navbar'),
                'data-title'       => lang('dh_term_navbar'),
                'data-visible'     => true,
                'data-width'       => 100,
            ],
            'term_controll' => [
                'order'           => 13,
                'type'            => 'select',
                'value'           => DcEmpty($data['term_controll'],'web'),
                'option'          => [
                                      'web'   => lang('dh_option_web'),
                                      'mp'    => lang('dh_option_mp'),
                                      'mini'  => lang('dh_option_mini'),
                                      'page'  => lang('dh_option_page'),
                                     ],
                'data-width'      => 100,
                'data-filter'     => true,
                'data-visible'    => true,
            ],
            'term_action' => [
                'order'           => 14,
                'type'            => 'select',
                'value'           => DcEmpty($data['term_action'],'index'),
                'option'          => ['index'=>lang('dh_option_index'),'channel'=>lang('dh_option_channel')],
                'data-width'      => 100,
                'data-filter'     => true,
                'data-visible'    => true,
            ],
            'term_count' => [
                'order'           => 15,
                'data-visible'    => true,
                'data-sortable'   => true,
                'data-width'      => '100',
            ],
            'term_order' => [
                'order'           => 16,
                'type'            => 'number',
                'value'           => intval($data['term_order']),
                'data-filter'     => false,
                'data-visible'    => true,
                'data-width'      => '100',
                'data-sortable'   => true,
            ],
            'term_tpl'   => [
                'order'            => 17,
                'type'             => 'text',
                'value'            => $data['term_tpl'],
            ],
            'html_3' => [
                'order'            => 18,
                'type'             => 'html',
                'value'            => '</div></div>',
            ]
        ];
        //表单扩展字段自动处理
        $extend = [];
        foreach(config('custom_fields.term_meta')  as $key=>$value){
            if( in_array('daohang',$value['attr-module']) ){
                $extend[$key] = $value;
            }
        }
        if($extend){
            $fields = DcArrayPush($fields, DcFields($extend, $data), 'term_title');
        }
        //返回所有表单字段
        return $fields;
    }
    
    /**
    * 重置表格选项通用属性处理
    * @version 1.0.0 首次引入
    * @param array $data 可选;初始数据;默认：空
    * @return array 表格列字段属性（DcBuildTable）
    */
    public function columns($data=[])
    {
        $columns = DcTableColumns($this->fields($data));
        
        $columns['operate']['data-events'] = 'daicuo.admin.daohang.eventsDialog';
        
        return $columns;
    }
    
    /**
    * 按内容模型获取分类关系
    * @version 1.2.0 首次引入
    * @param array $data 可选;初始数据;默认：空
    * @return array key=>value格式
    */
    public function option($controll='web'){
        $item = daohangCategorySelect([
            'module'   => 'daohang',
            'controll' => $controll,
            'action'   => 'index',
            'result'   => 'array',
            'with'     => '',
            'field'    => 'term_id,term_name',
        ]);
        $option = [];
        foreach($item as $key=>$value){
            $option[$value['term_id']] = $value['term_name'];
        }
        return $option;
    }
}